package com.gzhryc.shared_device.cdb.code.services;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.jdbc.Conditions;
import com.gzhryc.jdbc.JdbcSession;
import com.gzhryc.jdbc.MultiDBTools;
import com.gzhryc.shared_device.cdb.code.dao.PortableBatteryDao;
import com.gzhryc.shared_device.cdb.code.dao.db.BatteryCabinetSlot;
import com.gzhryc.shared_device.cdb.code.dao.db.PortableBattery;
import com.gzhryc.shared_device.cdb.code.dao.enums.PortableBatteryEnum;
import com.gzhryc.shared_device.cdb.code.services.dto.PortableBatterySearch;

import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PortableBatteryService {

	static Logger log = Logger.getLogger(PortableBatteryService.class);

	public static PortableBatteryService self() {
		return MultiDBTools.getService(PortableBatteryService.class, true);
	}

	public static PortableBatteryService self(String jdbcKey) {
		return MultiDBTools.getService(jdbcKey, PortableBatteryService.class, true);
	}

	PortableBatteryDao dao;

	public PortableBatteryService(String jdbcKey) {
		this.dao = new PortableBatteryDao(jdbcKey);
	}

	public List<PortableBattery> findBySearch(PortableBatterySearch search,Integer pageNo,Integer pageLimit){
		Conditions conditions = dao.buildConditions(search);
		try {
			return dao.findByCondition(conditions,pageNo,pageLimit);
		} catch (SQLException e) {
			log.error(e.getMessage(),e);
		}
		return null;
	}

	public Long countBySearch(PortableBatterySearch search){
		Conditions conditions = dao.buildConditions(search);
		try {
			return dao.count(conditions);
		} catch (SQLException e) {
			log.error(e.getMessage(),e);
		}
		return 0L;
	}

	public PortableBattery getByDeviceSn(String deviceSn){
		try {
			return dao.getById(deviceSn);
		} catch (SQLException e) {
			log.error(e.getMessage(),e);
		}
		return null;
	}

	public boolean add(PortableBattery portableBattery){
		portableBattery.setCreateDate(new Date());
		try {
			return dao.insert(portableBattery) > 0;
		} catch (SQLException e) {
			log.error(e.getMessage(),e);
		}
		return false;
	}

	public boolean eject(String deviceSn){
		if(StringTools.isNotBlank(deviceSn)) {
			Map<String, Object> data = new HashMap<>();
			data.put("deviceSn",deviceSn);
			data.put("cabinetDeviceSn",null);
			data.put("slotNum",null);
			data.put("state", PortableBatteryEnum.State.Lease.index());
			data.put("lastHeartDate",new Date());

			JdbcSession.begin();
			try {
				if(dao.update(data) > 0){
					BatteryCabinetSlotService.self().removePortableBatterySn(deviceSn);
					JdbcSession.commit();
					return true;
				}
			} catch (SQLException e) {
				log.error(e.getMessage(),e);
			}finally {
				JdbcSession.end();
			}
		}
		return false;
	}

	public boolean eject(String deviceSn,String tradeNo){
		if(StringTools.isNotBlank(deviceSn)) {
			Map<String, Object> data = new HashMap<>();
			data.put("deviceSn", deviceSn);
			data.put("cabinetDeviceSn", null);
			data.put("slotNum", null);
			data.put("currentTradeNo", tradeNo);
			data.put("state", PortableBatteryEnum.State.Lease.index());
			data.put("lastLeaseDate", new Date());

			JdbcSession.begin();
			try {
				if (dao.update(data) > 0) {
					BatteryCabinetSlotService.self().removePortableBatterySn(deviceSn);
					JdbcSession.commit();
					return true;
				}
			} catch (SQLException e) {
				log.error(e.getMessage(), e);
			} finally {
				JdbcSession.end();
			}
		}
		return false;
	}

	public boolean revert(String deviceSn,String cabinetDeviceSn,Integer slotNum,Double power){
		if(StringTools.isNotBlank(deviceSn)) {
			Map<String, Object> data = new HashMap<>();
			data.put("deviceSn",deviceSn);
			data.put("cabinetDeviceSn",cabinetDeviceSn);
			data.put("slotNum",slotNum);
			data.put("state", PortableBatteryEnum.State.Charging.index());
			data.put("power",power);
			data.put("lastHeartDate",new Date());

			JdbcSession.begin();
			try {
				if(dao.update(data) > 0){
					BatteryCabinetSlot batteryCabinetSlot = new BatteryCabinetSlot();
					batteryCabinetSlot.setDeviceSn(cabinetDeviceSn);
					batteryCabinetSlot.setSlotNum(slotNum);
					batteryCabinetSlot.setPortableBatterySn(deviceSn);

					if(BatteryCabinetSlotService.self().updateNotNull(batteryCabinetSlot)){
						JdbcSession.commit();
						return true;
					}else{
						log.error("修改卡槽信息失败，信息：{{0:json}}",batteryCabinetSlot);
					}
				}
			} catch (SQLException e) {
				log.error(e.getMessage(),e);
			} finally {
				JdbcSession.end();
			}
		}
		return false;
	}

	public boolean deleteByDeviceSn(String deviceSn) throws LogicException {
		try {
			return dao.deleteById(deviceSn) > 0;
		} catch (SQLException e) {
			log.error(e.getMessage(),e);
		}
		return false;
	}
}
